﻿Imports System
Imports System.IO.Ports
Imports System.Windows.Forms.DataVisualization.Charting


Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  '首先加载的函数

        Dim minValue, maxValue As DateTime

        minValue = DateTime.Now
        maxValue = minValue.AddSeconds(600)

        Chart1.ChartAreas.Clear()
        Dim ChartAreas1 As New ChartArea("Defult")    '定义新的ChartArea
        Chart1.ChartAreas.Add(ChartAreas1)
        Chart1.ChartAreas(0).AxisX.Title = "Time /s"   '设置ChartArea里坐标轴标题
        Chart1.ChartAreas(0).AxisY.Title = "Temp /℃"
        Chart1.ChartAreas(0).AxisX.Minimum = minValue.ToOADate() 'x轴幅度
        Chart1.ChartAreas(0).AxisX.Maximum = maxValue.ToOADate()
        Chart1.ChartAreas(0).AxisX.MajorGrid.LineColor = Color.LightGray 'x轴颜色
        Chart1.ChartAreas(0).AxisY.Minimum = 0 'y轴幅度
        Chart1.ChartAreas(0).AxisY.Maximum = 400
        Chart1.ChartAreas(0).AxisY.MajorGrid.LineColor = Color.LightGray 'y轴颜色

        Chart1.Series.Clear()
        Dim series1 As New Series("Expect Temp")
        Chart1.Series.Add(series1)
        series1.ChartType = SeriesChartType.Line '设置Series的绘图类型
        series1.BorderWidth = 1
        series1.Color = Color.Green
        series1.XValueType = ChartValueType.Time

        Dim series2 As New Series("Actual Temp")
        Chart1.Series.Add(series2)
        series2.ChartType = SeriesChartType.Line '设置Series的绘图类型
        series2.BorderWidth = 1
        series2.Color = Color.Red
        series2.XValueType = ChartValueType.Time

        Chart1.Legends(0).Docking = Docking.Right            '取消图例显示

        '----------------------------------------------------------------------

        Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间，用SerialPort,获取计算机的有效串口
        Dim port As String
        For Each port In ports
            portnamebox.Items.Add(port) '向combobox中添加项
        Next port

        SerialPort1.BaudRate = 115200 '波特率
        SerialPort1.DataBits = 8 '数据位
        SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
        SerialPort1.Parity = IO.Ports.Parity.None '校验位

    End Sub

    '每秒执行一次
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim rand As New Random
        Dim curent_time As DateTime

        curent_time = DateTime.Now

        Chart1.Series("Expect Temp").Points.AddXY(curent_time.ToOADate(), Val(TextBox1.Text))    '为这个Series加数据点
        If Chart1.Series("Expect Temp").Points.Count >= 595 Then   '采样点大于595个后

            Chart1.Series("Expect Temp").Points.RemoveAt(0) '删除曲线头个数据

            Dim ptA As ChartArea
            For Each ptA In Chart1.ChartAreas      '更新坐标轴时间
                ptA.AxisX.Minimum = Chart1.Series("Expect Temp").Points(0).XValue
                ptA.AxisX.Maximum = DateTime.FromOADate(Chart1.Series("Expect Temp").Points(0).XValue).AddSeconds(300).ToOADate()
            Next
        End If

        Chart1.Series("Actual Temp").Points.AddXY(curent_time.ToOADate(), Val(TextBox2.Text))    '为这个Series加数据点
        If Chart1.Series("Actual Temp").Points.Count >= 595 Then   '采样点大于295个后

            Chart1.Series("Actual Temp").Points.RemoveAt(0) '删除曲线头个数据

            Dim ptA As ChartArea
            For Each ptA In Chart1.ChartAreas      '更新坐标轴时间
                ptA.AxisX.Minimum = Chart1.Series("Actual Temp").Points(0).XValue
                ptA.AxisX.Maximum = DateTime.FromOADate(Chart1.Series("Actual Temp").Points(0).XValue).AddSeconds(600).ToOADate()
            Next
        End If
    End Sub

    '连接按键的处理
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If portnamebox.SelectedItem = "" Then
            MessageBox.Show("请选择设备接口！")
        Else
            If SerialPort1.IsOpen = False Then
                SerialPort1.PortName = portnamebox.SelectedItem  '串口名称
                Try
                    SerialPort1.Open() '打开串口
                    If SerialPort1.IsOpen = True Then
                        Label3.Text = "连接中"
                        Label3.ForeColor = Color.Green
                        Button1.Text = "断开"
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Else
                Try
                    SerialPort1.Close() '关闭串口
                    If SerialPort1.IsOpen = False Then
                        Label3.Text = "已断开"
                        Label3.ForeColor = Color.Red
                        Button1.Text = "连接"
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            End If
        End If
    End Sub

    '触发接收事件
    Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数
    End Sub

    '接收数据
    Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
        Dim strIncoming As String
        Dim temp
        Try
            Label5.Text = Str(Val(Label5.Text) + SerialPort1.BytesToRead) & "字节" '接收到的数据包计数
            If SerialPort1.BytesToRead > 0 Then
                Threading.Thread.Sleep(50) '延时50ms
                strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
                SerialPort1.DiscardInBuffer()
                temp = Split(strIncoming, ",")  '以“，”分割字符串，装入temp数组
                TextBox1.Text = temp(1)    '提取首个数据
                TextBox2.Text = temp(0)    '提取第二个数据
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            SerialPort1.Write((TextBox3.Text) & Chr(13) & Chr(10))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
End Class


